package com.itheima.a03;

import org.springframework.beans.factory.config.BeanPostProcessor;

import java.util.ArrayList;
import java.util.List;

public class TestMethodTemplate {

    public static void main(String[] args) {
        MyBeanFactory beanFactory = new MyBeanFactory();
        beanFactory.addBeanPostProcessor(() -> System.out.println("@AutoWired"));
        beanFactory.addBeanPostProcessor(() -> System.out.println("@Resource"));
        beanFactory.getBean();
    }

    // 模板方法  Template Method Pattern
    static class MyBeanFactory {
        public Object getBean() {
            Object bean = new Object();
            System.out.println("构造 " + bean);
            System.out.println("依赖注入 " + bean); // @Autowired, @Resource
            for (BeanPostProcessor beanPostProcessor : beanPostProcessors) {
                beanPostProcessor.inject();
            }
            System.out.println("初始化 " + bean);
            return bean;
        }
        List<BeanPostProcessor> beanPostProcessors = new ArrayList<>();
        public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor){
            beanPostProcessors.add(beanPostProcessor);
        }
    }
    static interface BeanPostProcessor{
        public void inject();
    }


}
